﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QcRiverFlows.Models;

namespace QcRiverFlows.Controllers
{
    public class FlowsController : Controller
    {
        private RiverFlowsContext db = new RiverFlowsContext();

        //
        // GET: /Flows/

        public ActionResult Index()
        {

            IEnumerable<Region> regions = db.Regions.ToList();

            var filter = new List<Region>();
            filter.Add(new Region() { Id = 9, Name = "Lanaudiere" });
            filter.Add(new Region() { Id = 11 , Name = "Mauricie" });

            IEnumerable<Flow> flows = db.Flows.Include(f => f.River).Include(f => f.Category).ToList();

            regions = (from r in regions
                     join f in filter on r.Id equals f.Id
                     select r
                            ).ToList();
                     
          


            return View(new Tuple<IEnumerable<Region>, IEnumerable<Flow>>(regions, flows));
        }

        //
        // GET: /Flows/Details/5

        public ActionResult Details(int id = 0)
        {
            Flow flow = db.Flows.Find(id);
            if (flow == null)
            {
                return HttpNotFound();
            }
            return View(flow);
        }

        //
        // GET: /Flows/Create

        public ActionResult Create()
        {
            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name");
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name");
            return View();
        }

        //
        // POST: /Flows/Create

        [HttpPost]
        public ActionResult Create(Flow flow)
        {
            if (ModelState.IsValid)
            {
                db.Flows.Add(flow);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name", flow.RiverId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", flow.CategoryId);
            return View(flow);
        }

        //
        // GET: /Flows/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Flow flow = db.Flows.Find(id);
            if (flow == null)
            {
                return HttpNotFound();
            }
            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name", flow.RiverId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", flow.CategoryId);
            return View(flow);
        }

        //
        // POST: /Flows/Edit/5

        [HttpPost]
        public ActionResult Edit(Flow flow)
        {
            if (ModelState.IsValid)
            {
                db.Entry(flow).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RiverId = new SelectList(db.Rivers, "Id", "Name", flow.RiverId);
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", flow.CategoryId);
            return View(flow);
        }

        //
        // GET: /Flows/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Flow flow = db.Flows.Find(id);
            if (flow == null)
            {
                return HttpNotFound();
            }
            return View(flow);
        }

        //
        // POST: /Flows/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Flow flow = db.Flows.Find(id);
            db.Flows.Remove(flow);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}